package es.udc.fbellas.j2ee.tiendaweb.model.administradorfacade.delegate;

import javax.sql.DataSource;

import es.udc.fbellas.j2ee.tiendaweb.model.util.GlobalNames;
import es.udc.fbellas.j2ee.util.configuration.ConfigurationParametersManager;
import es.udc.fbellas.j2ee.util.configuration.MissingConfigurationParameterException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.DataSourceLocator;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;


/**
 * A factory to get <code>UsuarioFacadeDelegate</code> objects.
 * <p>
 * Required configuration parameters:
 * <ul>
 * <li><code>UsuarioFacadeDelegateFactory/delegateClassName</code>: it must 
 * specify the full class name of the class implementing 
 * <code>UsuarioFacadeDelegate</code>.</li>
 * </ul>
 */
public final class AdministradorFacadeDelegateFactory {

    private final static String DELEGATE_CLASS_NAME_PARAMETER =
        "AdministradorFacadeDelegateFactory/delegateClassName";

    private static Class delegateClass;
    //private static String usuarioFacadeJNDIName;

    static {

        try {

            /* DELEGATE_CLASS_NAME_PARAMETER is specified in configuration? */        
            String delegateClassName = 
                ConfigurationParametersManager.getParameter(
                    DELEGATE_CLASS_NAME_PARAMETER);
            delegateClass = Class.forName(delegateClassName);
       
        } catch (MissingConfigurationParameterException e) {

            /* 
             * USER_FACADE_JNDI_NAME_PARAMETER should be specificed in
             * configuration.
             */
/*            try {

                userFacadeJNDIName = 
                       ConfigurationParametersManager.getParameter(
                    USER_FACADE_JNDI_NAME_PARAMETER);

            } catch (MissingConfigurationParameterException e2) {

                e.printStackTrace();
                e2.printStackTrace();
            }
*/
        } catch (Exception e) {
            e.printStackTrace();
        }
       

    }
    
    private AdministradorFacadeDelegateFactory() {}
    
    
    public static AdministradorFacadeDelegate getDelegate() 
        throws InternalErrorException {
        
        try {

//            if (delegateClass != null) {
                return (AdministradorFacadeDelegate) delegateClass.newInstance();
/*            } else {
                return ServiceLocator.findService(UserFacadeDelegate.class,
                    userFacadeJNDIName);
            }*/

        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
        
    }
    
    
    
      /* Test code. Uncomment for testing. Test this class and any class
     * implementing "UsuarioFacadeDelegate".
     */ 
        
    public static void main (String[] args) {
    
        try {

            /* Create a simple data source. */
            DataSource dataSource = new SimpleDataSource();

            /*
             * Add "dataSource" to "DataSourceLocator"
             * (this is needed to test
             * "es.udc.fbellas.j2ee.miniportal.model.userfacade.plain.
             * PlainUserFacadeDelegate"). 
             */
            DataSourceLocator.addDataSource(GlobalNames.TIENDAWEB_DATA_SOURCE,
                dataSource);
                
            /* Create an "UsuarioFacadeDelegate". */
            AdministradorFacadeDelegate delegate =
                AdministradorFacadeDelegateFactory.getDelegate();

System.out.println("\n\n\n");
System.out.println("***************************************************************");
System.out.println("*       PRUEBA DE UNIDAD PARA LA FACHADA ADMINISTRADOR        *");
System.out.println("***************************************************************");


System.out.println("------------------------------------------------------------------");
	    /* Test "ProductoFacadeDelegate::servirPedidos". */
			System.out.println("\nTest for " +
	          "'PedidoProductoFacadeDelegate::servirPedidos'");
				delegate.ServirPedidos("ADMIN","ssss");
				System.out.println("Pedidos Servidos");
System.out.println("------------------------------------------------------------------");

            /* Tests OK. */
            System.out.println("\t\tTests OK !!!!");

        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }        

}
